<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Roomnum extends BaseModel{

    protected $table = 'roomnum';
    public $timestamps = false;

    public function __construct( )
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    // 获取首页数据
    public function indexData($d){
         $data = [];
         $ca = $this->cateByRoom($d);
         // 获取正在直播的数据
         $LiveRoom = new Liveroom();
         $livedata = $LiveRoom->getOnlineRoom();
         $zixun = [['type'=>1,'limit'=>5],['type'=>2,'limit'=>5],['type'=>3,'limit'=>5]];
         // 获取资讯
         $Announce = new Announce();
         $announce = $Announce->getIndexAnnouce($zixun);
         $data = [
             'cate' => $ca,
             'livedata' => $livedata,
             'announce' => $announce,
         ];
         return $data;
    }

    // 根据分类获取数据
    public function cateByRoom($d){
         if (!$d) { return; }
         $d = $d->toArray();
         foreach ($d as $k => $v) {
             $where['cate'] = $v['id'];
             $room = $this->getList($where,'','',4);
             if ($room) {
                 $d[$k]['room'] = $room->toArray();
             }
         }
         return $d;
    }

    // 申请直播间
    public function applyRoom(){
        $roomtitle = $_POST['title'];
        $roomtype  = $_POST['type'];
        $tel       = $_POST['contact'];
        $intro     = $_POST['intro'];
        if (empty($roomtitle) || empty($roomtype) || empty($tel) || empty($intro)) {
                 $res['code'] = 400;
                 $res['msg']  = '参数为空';
                 return $res;
        }
        $data['num'] = implode('',unique_rand(0,9,8));
        $data['uid'] = session('loginId');
        $data['original'] = 'n';
        $data['cate'] = $roomtype;
        $data['tel']  = $tel;
        $data['addtime'] = time();
        $data['intro']   = $intro;
        $resulte = $this->add($data);
        if ($resulte) {
            $res['code'] = 200;
            $res['msg']  = '申请成功';
            return $res;
        }
        $res['code'] = 400;
        $res['msg']  = '申请失败';
        return $res;
    }

    /**
     * 修改房间详情
     */
    public function saveRoomeSet($request){
         $title = $request['title'];
         $announce = $request['announce'];
         $intro = $request['intro'];
         $data['announce'] = $announce;
         $data['title'] = $title;
         $data['intro'] = $intro;
         $where['num'] = $request['roomnum'];
         $where['uid'] = session('loginId');
         $result = $this->up($where,$data);
         if ($result) {
             $res['code'] = 200;
             $res['msg']  = '修改成功';
             return $res;
         }else{
             $res['code'] = 400;
             $res['msg']  = '修改失败';
             return $res;
         }

    }

    /**
     * 获取房间信息
     */
    public function getRoomInfo($num){
         $room = $this->getOne(['num'=>$num]);
         $room['isExist'] = 1;
         if (empty($room)){
              $room['isExist'] = 0;
              return $room;
              die;
         }
         $roomCateModel  = new Roomcategroy();
         $member = new Member();
         $room['cate']   = $roomCateModel->getOne(['id'=>$room['cate']]);
         $room['member'] =  $member->getOne(['id'=>$room['uid']]);
         $LiveRoom = new Liveroom();
         // 获取房间关注量
         $attention = new Attention();
         $attentionnum = $attention->getAttentionCount($room['uid']);
         $room['attentionnum'] = $attentionnum;
         // 获取房间开播状态
         $isLiveing = $LiveRoom->getOne(['roomnum'=>$num]);
         if (empty($isLiveing)){
              $room['isLive'] = 0;
         }else{
              $room['isLive'] = $isLiveing['status'];
              $push = $LiveRoom->getPushUrl(config('app.tx.bizid'),$num,config('app.tx.key'),date('Y-m-d H:i:s',time()));
              $room['pushur'] = $push;
              // 计算已直播的时间
              $time = time();
              $timediff = $time - $isLiveing['starttime'];
              $room['livetime'] = $timediff;
         }
         return $room;
    }

    /**
     * 判断用户是否为某房间主播
     */
    public function isRoomZhubo($req){
         $room = $req['room'];
         $uid = session('loginId');
         $roomInfo = $this->getOne(['num'=>$room]);
         if ( $roomInfo['uid'] == $uid ) {
              return 1;
         }
         return 0;
    }

    /**
     * 通过分类id获取直播间
     */
    public function getRoomdataBycate(){
          $where = [];
          if (isset($_GET['cid'])){
               $where['cate'] = $_GET['cid'];
          }
          $data = $this->getList($where);
          if (empty($data[0])){
               return [];
          }
          $member = new Member();
          $attention = new Attention();
          $roomcate = new Roomcategroy();
          foreach ($data as $key => $v) {
               $memberinfo = $member->getOne(['id'=>$v['uid']]);
               $cateinfo = $roomcate->getOne(['id'=>$v['cate']],'catename');
               $data[$key]['attention'] = $attention->getCount(['attuid'=>$v['uid']]);
               $data[$key]['member'] = $memberinfo;
               $data[$key]['catename'] = $cateinfo['catename'];
          }
          return $data;
    }
}